package com.pirestupppai.ui.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.imagetool.filtertool.FilterAdapter;
import com.pirestupppai.imagetool.filtertool.FilterItem;
import com.pirestupppai.imagetool.filtertool.FilterManager;
import com.pirestupppai.imagetool.filtertool.FilterSelectCallBack;
import com.pirestupppai.imagetool.filtertool.FilterView;
import com.pirestupppai.utils.WeakReferenceHandler;
import com.zomato.photofilters.SampleFilters;
import com.zomato.photofilters.imageprocessors.Filter;

import java.util.List;

import butterknife.BindView;

/**
 *  图片滤镜页面
 */

public class ImageFilterActivity extends BaseActivity{
    static {
        System.loadLibrary("NativeImageProcessor");
    }
    private WeakReferenceHandler mHandler = new WeakReferenceHandler(this);

    @BindView(R.id.filter_view)
    FilterView mFilterView;
    @BindView(R.id.filter_select_view)
    RecyclerView mImageSelectView;
    @BindView(R.id.complete_btn)
    ImageView mCompleteBtn;
    @BindView(R.id.back_btn)
    ImageView mBackBtn;

    private LinearLayoutManager mLinearLayoutManager;
    private FilterAdapter mFilterAdapter;
    private FilterCallBack mFilterCallBack;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected void initListeners() {
        mFilterCallBack = new FilterCallBack();
        mCompleteBtn.setOnClickListener(this);
        mBackBtn.setOnClickListener(this);
    }

    @Override
    protected void initViews() {
        mLinearLayoutManager = new LinearLayoutManager(mContext);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mImageSelectView.setLayoutManager(mLinearLayoutManager);
        mImageSelectView.setHasFixedSize(true);
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_image_filter;
    }

    @Override
    protected void initLogic() {
        mFilterView.setBackgroundBitmap(ImageEditActivity.mDeliverBitmap);
        bindDataToAdapter(ImageEditActivity.mDeliverBitmap);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete_btn:
                ImageEditActivity.mDeliverBitmap = mFilterView.getmBackgroundBitmap();
                setResult(RESULT_OK);
                this.finish();
                break;
            case R.id.back_btn:
                this.finish();
                break;
        }
    }

    private void bindDataToAdapter(final Bitmap bitmap) {
        Runnable r = new Runnable() {
            public void run() {
                Bitmap thumbImage = bitmap;
                FilterItem t1 = new FilterItem();
                FilterItem t2 = new FilterItem();
                FilterItem t3 = new FilterItem();
                FilterItem t4 = new FilterItem();
                FilterItem t5 = new FilterItem();
                FilterItem t6 = new FilterItem();

                t1.image = thumbImage;
                t2.image = thumbImage;
                t3.image = thumbImage;
                t4.image = thumbImage;
                t5.image = thumbImage;
                t6.image = thumbImage;

                t1.filterName = "原图";
                t2.filterName = "原图";
                t3.filterName = "原图";
                t4.filterName = "原图";
                t5.filterName = "原图";
                t6.filterName = "原图";

                FilterManager.clearThumbs();
                FilterManager.addThumb(t1); // Original Image

                t2.filter = SampleFilters.getStarLitFilter();
                FilterManager.addThumb(t2);

                t3.filter = SampleFilters.getBlueMessFilter();
                FilterManager.addThumb(t3);

                t4.filter = SampleFilters.getAweStruckVibeFilter();
                FilterManager.addThumb(t4);

                t5.filter = SampleFilters.getLimeStutterFilter();
                FilterManager.addThumb(t5);

                t6.filter = SampleFilters.getNightWhisperFilter();
                FilterManager.addThumb(t6);

                List<FilterItem> thumbs = FilterManager.processThumbs(mContext);

                mFilterAdapter = new FilterAdapter(thumbs, mContext, mFilterCallBack);
                mImageSelectView.setAdapter(mFilterAdapter);
                mFilterAdapter.notifyDataSetChanged();
            }
        };
        mHandler.post(r);
    }

    // 滤镜选择
    class FilterCallBack implements FilterSelectCallBack {
        @Override
        public void onSelectClick(Filter filter) {
            // 转换Bitmap为mutable
            Bitmap originBitmap = ImageEditActivity.mDeliverBitmap;
            Bitmap translateBitmap = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(translateBitmap);
            canvas.drawBitmap(originBitmap, 0, 0, null);

            mFilterView.setBackgroundBitmap(filter.processFilter(translateBitmap));

            mFilterAdapter.notifyDataSetChanged();
        }
    }
}
